package com.demo.flight.service.impl;

//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.flight.entity.Customer;
import com.demo.flight.mapper.CustomerMapper;
import com.demo.flight.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ssy
 * @create 2022-03-23 10:15
 */
@Service
public class CustomerServiceImpl implements CustomerService {
    
    @Autowired
    private CustomerMapper customerMapper;
    
    @Override
    public int countByCondition(Customer customer) {
        return customerMapper.countByCondition(customer);
    }
    
    @Override
    public List<Customer> getAll() {
        return customerMapper.selectAll();
    }
    
    @Override
    public List<Customer> getAllByCondition(Customer customer) {
        // 如果传入空值，返回所有用户
        return customerMapper.selectByCondition(customer);
    }
    
    @Override
    public boolean add(Customer customer) {
//        如果信息完整而且没有重复
        if (checkComplement(customer) && !checkIsDuplicated(customer)) {
            System.out.println(customer);
            customerMapper.insert(customer);
            return true;
        }
        return false;
    }
    
    @Override
    public boolean delete(Integer customerId) {
        if (customerId != null) {
            int count = customerMapper.countById(customerId);
            if (count == 1) {
                customerMapper.deleteById(customerId);
                return true;
            }
        }
        return false;
    }
    
    @Override
    public boolean update(Customer customer) {
        int count = customerMapper.countById(customer.getCustomerId());
        if (count == 1 && checkComplement(customer)) {
            customerMapper.update(customer);
            return true;
        }
        return false;
    }
    
    @Override
    public boolean checkComplement(Customer customer) {
        if (customer != null) {
            String customerName = customer.getCustomerName();
            String gender = customer.getGender();
            Integer age = customer.getAge();
            String address = customer.getAddress();
            String phone = customer.getPhone();
            return customerName != null && gender != null && age != null && address != null && phone != null;
        }
        return false;
    }
    
    @Override
    public boolean checkIsDuplicated(Customer customer) {
        if (customer == null) {
            return true;
        } else {
            // 是否存在信息完全一致的客户
            return customerMapper.countByCondition(customer) > 0;
        }
    }
}
